﻿package model.drawPanel.entitys;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Display;
import org.eclipse.wb.swt.SWTResourceManager;


public class Picture extends Entity {
	/**
	 * 
	 */
	private static final long serialVersionUID = 101250105L;
	private byte[] data;//图片最大1M
	private transient Image image;
	private transient FileInputStream filein;
	private transient Image halfImage;
public Picture(int id, int x1, int y1,File file) {
		super(id, null, -1, -1, x1, y1, -1, -1);
		image = SWTResourceManager.getImage(file.getAbsolutePath());
		this.x2 =image.getBounds().width + x1;
		this.y2= image.getBounds().height + y1;
	/*	try {
			filein= new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			filein.read(data);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		image =SWTResourceManager.getImage(file.getAbsolutePath());
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ImageLoader imageLoader = new ImageLoader();
		imageLoader.data = new ImageData[] { image.getImageData() };
		imageLoader.save(baos, SWT.IMAGE_JPEG);
		data =baos.toByteArray();
	}
@Override
public void draw(Display display, GC gc) {
	if(image==null){
		initeImage();
	}
	if(isChoosed){
		gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
		gc.setLineWidth(3);
		gc.setLineStyle(SWT.LINE_SOLID);
		gc.drawOval(x1-6, y1-6, 12, 12);
		gc.drawOval(x2-6, y1-6, 12, 12);
		gc.drawOval(x1-6, y2-6, 12, 12);
		gc.drawOval(x2-6, y2-6, 12, 12);
		if(halfImage ==null){
			ImageData data =image.getImageData();
			data.scaledTo(80, 60);
			data.alpha = 128;
			halfImage = new Image(display,data);
		}
		gc.drawImage(halfImage,0,0,image.getBounds().width,image.getBounds().height ,Math.min(x1,x2), Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));
	}else{
		gc.drawImage(image,0,0,image.getBounds().width,image.getBounds().height ,Math.min(x1,x2), Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));
	}
}
private void initeImage(){
	ByteArrayInputStream byteBuffer = new ByteArrayInputStream(data);
	image = new Image(Display.getDefault(),byteBuffer);	
	
}

@Override
public String isFocused(int x, int y) {
	super.isFocused(x, y);
	if(image==null){
		initeImage();
	}
	if(isChoosed){
		if(distance(x1,y1,x,y) <= 6){
			return "zoomleftup";
		}
		if(distance(x1,y2,x,y) <= 6){
			return"zoomleftdown";
		}
		if(distance(x2,y1,x,y) <= 6 ){
			return"zoomrightup";
		}
		if(distance(x2,y2,x,y) <= 6){
			return"zoomrightdown";
		}
	}
	org.eclipse.swt.graphics.Rectangle  bounds = image.getBounds();
	if(x1<=x && x2 >=x && y1 <=y & y2 >= y){	
		return"entity";
	}
	
	return "noEnter";
}

public Image getImage() {
	return image;
}
public void setImage(Image image) {
	this.image = image;
}

}